<?php   
class feed_controller extends CI_Controller
{
	function __construct()
	{	
		parent::__construct();
		
		$temp = $this->session->userdata('is_logged_in');
		if( !isset($temp) || !$temp ) exit('No direct script access allowed');
		
		$this->load->model('hello_model');
		$this->load->model('apply_model');
		$this->load->model('user_model');
		$this->load->model('area_model');
		$this->load->model('geolocation_model');
	}
	function index()
	{
		$data['title']="Hellogue";
		$data['id']="feed";
		$data['rows'] = $this->hello_model->ChangeQueryResultToArray($this->hello_model->get_all_with_fullname());

		$this->load->view('header',$data);
		$this->load->view('feed_view', $data);
		$this->load->view('footer',$data);
	}
	
	function my_hello()
	{
		$data['title']="My Hello";
		$data['id']="myhello";
		$profile['userid']=$this->session->userdata('userid');
		$data['rows'] = $this->hello_model->ChangeQueryResultToArray($this->hello_model->get_hello_with_fullname($profile));
		//$data['rows'] = $this->hello_model->ChangeQueryResultToArray($this->hello_model->get_all_with_fullname());
		$data['my_hello'] = true;
		

		$this->load->view('header',$data);
		$this->load->view('feed_view', $data);
		$this->load->view('footer',$data);
	}
	
	
	function to_apply()
	{
		$profile['helloid']= $this->input->get('helloID');
		$this->session->set_userdata($profile);
		
		$Qresult = $this->hello_model->ChangeQueryResultToArray( $this->hello_model->get_hello_for_apply( $this->input->get('helloID') ) );

		$data['title'] = "Hello";
		$data['id']="";
		$data['row'] = $Qresult[0];
		
	$data['guests'] = $data['guests'] = $this->hello_model->ChangeQueryResultToArray(
											 $this->hello_model->get_guests($profile['helloid']));
		
		if ( $data['row']->guestid == $this->session->userdata('userid') )
		{
			$data['id']="confirmed_hello";
		}
			
		//This line is for I'll be there bug
		$data['is_exist'] = $this->apply_model->is_exist($profile['helloid']);
		
		$this->load->view('header',$data);
		$this->load->view('hello_apply_view',$data);
		
	}
	
	
	function apply()
	{
		$helloid = $this->input->get('helloID');
		
		if ( $this->apply_model->link($helloid) )
			redirect('notification_controller/send_notice?notice_type=1');
		else
			redirect('feed_controller');
	}
	

	function read_applicants()
	{
		$profile['helloid']= $this->input->get('helloID');
		$data['rows'] = $this->apply_model->ChangeQueryResultToArray( $this->apply_model->get_apply_with_fullname( $this->input->get('helloID') ) );

		$data['helloid'] = $profile['helloid'];
		$data['id']="readApplicants";
		$data['title']="Confirm Guest";
		$this->load->view('header',$data);
		$this->load->view('read_applicants_view',$data);
		
	}

	function confirm_guest()
	{
		//many things to do
		$profile['guestid'] = $this->input->get('userID');
		$profile['helloid'] = $this->input->get('helloID');
  
		$this->db->from('hello');
		$this->db->where('helloid', $profile['helloid']);
		$query=$this->db->get();
  
		//print_r($query);
		$temp = $query->result();
		$profile['number_of_guests']=$temp[0]->number_of_guests+1;
		$profile['isconfirmed'] = ($profile['number_of_guests']==$temp[0]->quota);
		$this->apply_model->guest_confirm($profile['helloid'],$profile['guestid']);
		
		$this->hello_model->update($profile);
		$data['notification_guestid']=$this->input->get('userID');
		$this->session->set_userdata($data);
		//send notification to all applicants
		redirect('notification_controller/send_notice?notice_type=2');
	}

	function search()
	{
		
		$searchwords = explode(" ", $this->input->post('searchwords'));
		$data['rows']=$this->hello_model->ChangeQueryResultToArray($this->hello_model->search($searchwords));
		$data['title']="Search Result";
		$data['id']="search";
		
		$this->load->view('header',$data);
		$this->load->view('feed_view', $data);
		$this->load->view('footer',$data);
		
	}
	function feed_search()
	{
		$temp = $this->input->post('but');
		if($temp=='Search' )
			$this->search();
		else
			$this->to_lucky();
			
	}
	function choose_place()
	{
		$data['lat']=$this->input->get('lat');
		$data['longi']=$this->input->get('longi');
		echo $data['lat'];
		echo $data['longi'];
		$data['title']="Post New Hello";
		$data['id']="";
		$data['area']=$this->area_model->ChangeQueryResultToArray($this->area_model->get_near($data['lat'],$data['longi']) );
		$this->load->view('header',$data);
		$this->load->view('choose_place_view',$data);
		$this->load->view('footer',$data);
	}
	function to_lucky()
	{
		$d['status_create'] = false;
		$this->session->set_userdata($d);
		$data['title']="Lucky Place";
		$data['id']="lucky";
		
		$this->load->view('header',$data);
		$this->load->view('map',$data);
	}
}
?>
